﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Film8.DAL;
using Film8.Models;

namespace Film8.Controllers
{
    [Authorize]
    public class MovieController : Controller
    {
        private Film8Context db = new Film8Context();

        // GET: Movie
        public ActionResult Index(string searchString,string searchStringGenre,string searchStringYear)
        {
            var movies = from s in db.Movies
                       select s;
            if (!String.IsNullOrEmpty(searchString))
            {
               movies = movies.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));

            }
            if (!String.IsNullOrEmpty(searchStringGenre))
            {
                movies = movies.Where(s => s.Genre.ToUpper().Contains(searchStringGenre.ToUpper()));

            }
            if (!String.IsNullOrEmpty(searchStringYear))
            {
                movies = movies.Where(s => s.Year.ToString().ToUpper().Contains(searchStringYear.ToUpper()));

            }
           
            

            if (Request.IsAjaxRequest())
            {
                return PartialView("_MoviesTable", movies.ToList());
            }
            return View(movies.ToList());
        }

      // GET: Movie/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

       // GET: Movie/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Movie/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Title,Genre,Year")] Movie movie)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Movies.Add(movie);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch(Exception)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return View(movie);
        }

        // GET: Movie/Edit/5
    [Authorize(Users="admin@film8.com")]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        // POST: Movie/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Title,Genre,Year")] Movie movie)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(movie).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); }
           
        
            return View(movie);
        }
         [Authorize(Users = "admin@film8.com")]
        // GET: Movie/Delete/5
        public ActionResult Delete(int? id,bool? saveChangesError=false)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        // POST: Movie/Delete/5
        [Authorize(Users = "admin@film8.com")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            try
            {
                Movie movie = db.Movies.Find(id);
                db.Movies.Remove(movie);
                db.SaveChanges();
            }
            catch(Exception)
            {
                return RedirectToAction("Delete", new { id = id, saveChangesError = true });
            }
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
